package com.cpjit.data;

import com.cpjit.data.annotation.FieldNameMatch;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import org.apache.ibatis.mapping.SqlCommandType;

import java.util.Objects;

/**
 * @author yonghuan[cpjit@qq.com]
 * @see FieldNameMatch
 */
@Getter
@ToString
@AllArgsConstructor
final class FieldNameMatcher implements AuditingMatcher {

    private final String fieldName;
    private final SqlCommandType sqlCommandType;

    @Override
    public boolean matches(AuditingContext ctx) {
        Object target = ctx.getTarget();
        if (target instanceof FieldNameMatch annotation) {
            return ctx.getSqlCommandType() == sqlCommandType
                    && Objects.equals(annotation.value(), fieldName);
        }
        return false;
    }
}
